package com.weilive.core.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

public final class DateUtil {
	private DateUtil() {
	}

	public static final int DAY = 1;
	public static final int HOUR = 2;
	public static final int MINITE = 3;
	public static final int SECOND = 4;
	public static final int MONTH = 5;

	public static final int DAY_SECOND = 24 * 60 * 60;

	public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

	public static String dateFormat(Date date) {
		return dateFormat(date, DEFAULT_DATE_FORMAT);
	}

	public static String dateFormat(Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		String str = sdf.format(date);
		return str;
	}

	public static Date dateFormat(String dateStr) {
		return dateFormat(dateStr, DEFAULT_DATE_FORMAT);
	}

	public static Date dateFormat(String dateStr, String format) {
		if (StringUtils.isBlank(dateStr)) {
			return null;
		}
		DateFormat dateFormat = new SimpleDateFormat(format);
		Date date = null;
		try {
			date = dateFormat.parse(dateStr);
		} catch (ParseException e) {
			return null;
		}

		return date;
	}

	// 每天4点的时候更新数据
	public static boolean isTheSameDay(Date d1, Date d2) {
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c1.setTime(d1);
		c2.setTime(d2);

		return (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR)) && (c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH))
				&& (c1.get(Calendar.DATE) == c2.get(Calendar.DATE));
	}

	public static boolean isToday(Date lastDate) {
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c1.setTime(lastDate);
		return (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR)) && (c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH))
				&& (c1.get(Calendar.DATE) == c2.get(Calendar.DATE));
	}

	public static boolean isYesterday(Date lastDate) {
		return isYesterday(lastDate, new Date());
	}

	public static boolean isYesterday(Date lastDate, Date date) {
		Calendar last = Calendar.getInstance();
		Calendar now = Calendar.getInstance();
		last.setTime(lastDate);
		now.setTime(date);
		last.add(Calendar.DAY_OF_MONTH, 1);
		return (last.get(Calendar.YEAR) == now.get(Calendar.YEAR))
				&& (last.get(Calendar.MONTH) == now.get(Calendar.MONTH))
				&& (last.get(Calendar.DATE) == now.get(Calendar.DATE));
	}

	public static Date getZeroTime() {
		Calendar begin = Calendar.getInstance();

		begin.set(Calendar.HOUR_OF_DAY, 0);
		begin.set(Calendar.MINUTE, 0);
		begin.set(Calendar.SECOND, 0);
		return begin.getTime();
	}

	public static Date getTodayEndTime() {
		Calendar begin = Calendar.getInstance();
		begin.set(Calendar.HOUR_OF_DAY, 23);
		begin.set(Calendar.MINUTE, 59);
		begin.set(Calendar.SECOND, 59);
		return begin.getTime();
	}

	public static boolean isExpired(Date last, int coolSecond) {
		if (null == last || coolSecond <= 0) {
			return true;
		}
		Date coolPassDate = addTime(SECOND, coolSecond, last);

		return coolPassDate.before(new Date());
	}

	public static boolean isExpired(Date last) {
		if (null == last) {
			return true;
		}
		Date now = new Date();

		return last.before(now);
	}

	public static Date addTime(int param, int add) {
		return addTime(param, add, new Date());
	}

	public static Date addTime(int param, int add, Date date) {
		if (null == date) {
			return null;
		}
		Calendar begin = Calendar.getInstance();
		begin.setTime(date);

		if (DAY == param)
			begin.add(Calendar.DATE, add);
		if (HOUR == param)
			begin.add(Calendar.HOUR_OF_DAY, add);
		if (MINITE == param)
			begin.add(Calendar.MINUTE, add);
		if (SECOND == param)
			begin.add(Calendar.SECOND, add);
		if (MONTH == param) {
			begin.add(Calendar.MONTH, add);
		}

		return begin.getTime();
	}

	public static long getSecondsToGoFromNow(Date fromDate) {
		if (null == fromDate) {
			return -1L;
		}
		Date now = new Date();
		if (now.before(fromDate)) {
			return 0L;
		}
		return (now.getTime() - fromDate.getTime()) / 1000L;
	}

	public static long getSecondsToGo(Date fromDate, Date toDate) {
		if ((null == fromDate) || (null == toDate)) {
			return -1L;
		}
		if (toDate.before(fromDate)) {
			return 0L;
		}
		return (toDate.getTime() - fromDate.getTime()) / 1000L;
	}

	public static int getWeekDay() {
		Calendar cal = Calendar.getInstance();

		return cal.get(Calendar.DAY_OF_WEEK) - 1;
	}

	public static void main(String[] args) {
		// Calendar c1 = Calendar.getInstance();
		// System.out.println(c1.get(Calendar.HOUR_OF_DAY));
		Date startTime = dateFormat("2011-02-18 08:00:00");

		System.out.println(isExpired(startTime));
		
		
		System.out.println(dateFormat(addTime(MONTH, 1, new Date())));

		int day = 1;
		if (!isToday(startTime)) {
			day = day + 1;
			if (!isYesterday(startTime)) {
				day = 0;
			}
		}
		System.out.println(day);
	}
}
